import { getControllers } from '../utils/getPath.js';
import { createRouter } from '../utils/router.js';
import cors from '@koa/cors'
import Router from 'koa-router';
import bodyparser from 'koa-bodyparser';

let router = new Router();

export default (apply) => {
    // Solve cross-domain problems
    apply.use(
        cors()
        // cors({
        //   // 允许的前端域名和端口
        //   origin: "http://localhost:8000",
        //   // 允许的 HTTP 方法
        //   methods: ["GET", "POST", "PUT", "DELETE"],
        //   // 允许的请求头
        //   allowedHeaders: ["Content-Type", "Authorization"],
        // })
    );
    
    // decler json object
    apply.use(bodyparser());
    // Scan controller folder
    let controllerFiles = getControllers();

    // 遍历控制器文件导入，拿到控制器参数数组注册路由
    createRouter(router, controllerFiles);
    
    console.log(`TEXT! Router: ${router}`);

    apply.use(router.routes())
}